﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeifenLuo.WinFormsUI.Docking;
using System.Xml.Serialization;
using System.IO;
using SoundEdge.Common;
using SoundEdge.Media.Project;
using SoundEdge.Media.Filter;
using SoundEdge.Media;

namespace SoundEdge.Dashboard.Project
{
    [Serializable]
    public class SoundEdgeProject : ISoundEdgeProject
    {
        [NonSerialized]
        internal frmDesign _designer;

        [NonSerialized]
        internal string _fileName;

        [NonSerialized]
        internal bool _saved;

        [NonSerialized]
        private List<IWaveFilter> _filters;

        [NonSerialized]
        private string _workingFolder;

        public Guid ID { get; set; }

        [XmlIgnore]
        public IWaveViewer Viewer { get; set; }

        [XmlIgnore]
        public string FileName
        {
            get { return _fileName; }
        }

        [XmlIgnore]
        public string WorkingFolder
        {
            get { return _workingFolder; }
        }
        
        //[XmlIgnore]
        public List<IWaveFilter> Filters
        {
            get { return _filters; }
            set { _filters = value; }
        }

        public string Title
        {
            get
            {
                string title = _designer != null ? _designer.Text : "";
                int idx = title.LastIndexOf(" *");
                if (idx > 0)
                {
                    title = title.Substring(0, idx);
                }

                return title;
            }
        }

        public SoundEdgeProject()
            : this(null, null)
        {
        }

        public SoundEdgeProject(frmDesign designForm, DockPanel dockPanel)
        {
            ID = Guid.NewGuid();
            if (designForm != null)
            {
                SetDesignerForm(designForm, dockPanel, false);
            }
            _filters = new List<IWaveFilter>();
            _workingFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
        }

        public void SetDesignerForm(frmDesign designForm, DockPanel dockPanel, bool saved)
        {
            _designer = designForm;
            _designer.Show(dockPanel, DockState.Document);

            if (!saved)
            {
                _designer.Text += " *";
            }
            _saved = saved;
        }

        public void Save(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName))
            {
                return;
            }
            string xml = XmlSerialization.ToXML(this);
            File.WriteAllText(fileName, xml);

            _fileName = fileName;

            string fn = Path.GetFileNameWithoutExtension(fileName);
            _designer.Text = fn;
            _saved = true;
        }

        public static ISoundEdgeProject Load(string fileName, frmDesign designForm, DockPanel dockPanel)
        {
            if (!File.Exists(fileName))
            {
                return null;
            }

            string xml = File.ReadAllText(fileName);

            SoundEdgeProject prj = XmlSerialization.FromXML(xml, typeof(SoundEdgeProject)) as SoundEdgeProject;
            prj._fileName = fileName;
            if (designForm != null)
            {
                prj.SetDesignerForm(designForm, dockPanel, true);
            }

            return prj;
        }

    }
}
